package xyz.fuqufyb.aec.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;


import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.spingcloud.travel.common.JsonResult;
import com.spingcloud.travel.common.TableResult;
import com.springcloud.travel.entity.Theme;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;


import org.springframework.web.multipart.MultipartFile;

import xyz.fuqufyb.aec.feign.theme.ThemeFeign;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/theme")
public class SysThemeController {

    @Resource
    private ThemeFeign themeFeign;
    @Autowired
    private ObjectMapper objectMapper;

    private static String url;

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @RequestMapping("/table")

    public TableResult showAll(@RequestParam Map<String,String> map){

        System.out.println("aaaaa");
        TableResult<Theme> themeTableResult = themeFeign.selectAllTheme(map);
        if (themeTableResult==null){
            return TableResult.getFail("查询失败");
        }
//       PageInfo pageInfo=new PageInfo(themes);
        return themeTableResult;
//        String repalceContent=null;
//        for (Theme theme : themes) {
//            String content = theme.getThemeContent();
//            repalceContent=content.replaceAll("\\\\","");
//            theme.setThemeContent(repalceContent);
//        }
////        if (list==null||list.size()<=0){
////            return JsonResult.getJsonResultByFail("查询失败");
////        }
//        return JsonResult.getJsonResultBySuccess("查询成功",themes);
    }

    @RequestMapping("/html")

    public JsonResult showAll(String act, Theme theme) throws JsonProcessingException {
        if (theme.getThemeType()==null||"".equalsIgnoreCase(theme.getThemeType())){
            return null;
        }
        System.out.println(act);
        theme.setUpdateTime(new Date());
        if ("".equals(theme.getThemeContent())){
            theme.setThemeContent(null);
        }
//        System.out.println(theme);
        String content=theme.getThemeContent();
        String json = objectMapper.writeValueAsString(content);
        theme.setThemeContent(json);

//        content.replaceAll("<","\\<");
//        content.replaceAll(">","\\>");
        if ("add".equalsIgnoreCase(act)){
            System.out.println(url);
            theme.setThemePicture(url);
            boolean add = themeFeign.add(theme);
            if(add){
                return JsonResult.getJsonResultBySuccess("操作成功",true);
            }
        }else {
            theme.setThemePicture(url);
            boolean update = themeFeign.update(theme);
            if (update){
                return JsonResult.getJsonResultBySuccess("操作成功");
            }
        }

       return JsonResult.getJsonResultByFail("操作失败");

    }

    @RequestMapping("/upload")
    public Map<String, Object> upload(MultipartFile file){
        Map<String, Object> map=new HashMap<>();
        Map<String, Object> map2=new HashMap<>();

        String originalFilename = file.getOriginalFilename();
        System.out.println(originalFilename);
        //上传
        try {
            StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(originalFilename), null);
            if (storePath!=null){
                String fullPath = storePath.getFullPath();
                url="http://101.132.226.166:8888/"+fullPath;
                map.put("msg","ok");
                map.put("code",0);
                map2.put("src","http://101.132.226.166:8888/"+fullPath);
                map.put("data",map2);
                return map;
            }
            map.put("msg","fail");
            map.put("code",200);
            map.put("data",null);
            return map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }




}
